package cn.bgs.utils;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class FileUtil { public static String downloadFile(HttpServletResponse response, String filePath) {
    String[] strArray=filePath.split("/");
    String filename=strArray[strArray.length-1];
    response.setHeader("Access-Control-Expose-Headers","Content-Disposition");
    response.setCharacterEncoding("UTF-8");
    response.setHeader("content-type", "application/octet-stream");
    response.setContentType("application/octet-stream");
    try {
        response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(filename, "UTF-8"));
    } catch (UnsupportedEncodingException e2) {
        e2.printStackTrace();
    }
    byte[] buff = new byte[1024];
    BufferedInputStream bis = null;
    OutputStream os = null;
    try {
        os = response.getOutputStream();
        bis = new BufferedInputStream(new FileInputStream(new File(filePath)));
        int i = bis.read(buff);
        while (i != -1) {
            os.write(buff, 0, buff.length);
            os.flush();
            i = bis.read(buff);
        }
    } catch (FileNotFoundException e1) {
        //e1.getMessage()+"系统找不到指定的文件";
        return "系统找不到指定的文件";
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return "success";
}

}
